<?php
/**
 *file-level documentation
 *@author Name <email@exp.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, Tuncay Bakan
 *@package somePackage
 *
 */
/**
 *shows the home site
 */
class Product_Controller extends Controller{
    /**
     * Holds id of page to be displayed
     * @var integer
     */
    private $iPage = '';
    /**
     * Language in wich to show page
     * @var integer
     */
    private $iLang ='';
        /**
     * Determines the level from which to get the page structure
     * @var mixed
     */
    private $mNavigationRoot = 'NULL';
    /**
     * Sets the template
     * @var string
     */
    private $sTemplate ='productCategories';
  /**
     * Reference to model that retrieves page data from db
     * @var object
     */
    private $oContentModel;
    
    /**
     * Reference to model that retrieves translated texts from db
     * @var object
     */
    private $oTextModel;

    /**
     * Reference to view of content section
     * @var object
     */
    private $oContentView;

    /**
     * Reference to model that retrieves page navigation elements from db
     * @var object
     */
    private $oNavigationModel;

    /**
     * Reference to view of navigation section
     * @var object
     */
    private $oNavigationView;

    /**
     * Reference to view of left navigation section in cms, it shows the entire pagestructure
     * @var object
     */
    private $oLeftNavigationView;
    /**
     * Ensures that the user is able to see where he is located within the homepage
     * @var object
     */
    private $oLocationView;

    public function __construct(){
        //Gets Viewmanger reference from parent constructor
        parent::__construct();
        //Loads models and views
        $this->oContentModel = Loader::loadModel('productPage');
        $this->oNavigationModel = Loader::loadModel('navigation');
        $this->oTextModel = Loader::loadModel('textLanguage');
        $this->oLocationView = $this->oViewmanager->getView('location');
        $this->oNavigationView = $this->oViewmanager->getView($GLOBALS['NAVIGATIONTEMP']);
        $this->oLeftNavigationView = $this->oViewmanager->getView('leftnavigation');
        //Checks language set in Cookie, take default language specified in config if not set
        if (isset($_COOKIE['language'])){
            $this->iLang = intval($_COOKIE['language']);
        }else{
            $this->iLang = "1";
        }
    }

    public function checkArgs($aArgs){
        //Reads out the product page or category page which should be loaded
        $this->iPage = array_shift($aArgs);
         //Checks if iPage is a category page and set the template to productList
        $bIsCategory = $this->oContentModel->isCategory($this->iPage);
        if($bIsCategory){
            $this->sTemplate = 'productList';
        }

        if($this->iPage != DEFAULT_STARTPAGE){
            $this->mNavigationRoot = $this->iPage;
        }
        //Loads the template which was set
        $this->oContentView = $this->oViewmanager->getView("$this->sTemplate");
        // ID is the product ID if set load the product detail template
        if (isset($aArgs['id'])){
            $iProductID = intval($aArgs['id']);
            $this->sTemplate = 'productDetail';
            $this->oContentView = $this->oViewmanager->getView("$this->sTemplate");
            $this->oContentView->assign('content', $this->oContentModel->getProductDetail($iProductID,$this->iLang));
            $this->oContentView->assign('images', $this->oContentModel->getProductImages($iProductID));
            $this->oLocationView->assign('location', $this->oNavigationModel->getLocation($iProductID, $this->iLang, 'product'));
            
            //get label for priceRequest button in the correct language
            $this->oContentView->assign('priceRequestLabel', $this->oTextModel->getTextByName('priceRequest', $this->iLang));
            //If it's a category load all products of the category
        }elseif($bIsCategory){
            $aProducts = $this->oContentModel->getProducts($this->iPage, $this->iLang);
            $aProductImages = array();
            if (is_array($aProducts)){
            foreach ($aProducts as $aProduct){
                $aImages = $this->oContentModel->getProductImages($aProduct['pid']);
                if(!empty($aImages)){
                $aProductImages[$aProduct['pid']] = $aImages;
                }else{
                    $aProductImages[$aProduct['pid']] = DEFAULT_PRODUCT_IMAGE;
                }
            }
            }
            $this->oContentView->assign('content', $aProducts);
            $this->oContentView->assign('productImages', $aProductImages);
            $this->oContentView->assign('productPageID', $this->oContentModel->getParentProductPage($this->iPage));
            $this->oLocationView->assign('location', $this->oNavigationModel->getLocation($this->iPage, $this->iLang, 'contentPage'));
            //If it's neither a category or a product the product categories will be loaded
        }else{
            $this->oContentView->assign('content', $this->oContentModel->getCategories($this->iPage, $this->iLang));
            $this->oLocationView->assign('location', $this->oNavigationModel->getLocation($this->iPage, $this->iLang, 'contentPage'));
        }

    }
/**
 * Executes controller methods
 * @param array different arguments
 */
    public function execute($aArgs){
        //Checks the arguments in the method checkArgs
        $this->checkArgs($aArgs);
        //Passes data to view
        $this->oContentView->assign('title', $this->oContentModel->getPageTitle($this->iPage,$this->iLang));
        //Checks if mobile device was used
        if($GLOBALS['ISMOBILE']==1){
            $this->oNavigationView->assign('navigation', $this->oNavigationModel->getPageStructure($this->mNavigationRoot,$this->iLang));
        }else{
            $this->oNavigationView->assign('navigation', $this->oNavigationModel->getPageStructure('NULL',$this->iLang));
        }

        $this->oLeftNavigationView->assign('productsANDcategories', $this->oNavigationModel->getProdCat('',$this->iLang, 'product'));
        //Inserts data in the correct sections of the website layout
        $this->oViewmanager->setSection('Location', $this->oLocationView->display());
        $this->oViewmanager->setSection('Title', $this->oContentModel->getPageTitle($this->iPage, $this->iLang));
        $this->oViewmanager->setSection('Content', $this->oContentView->display());
        $this->oViewmanager->setSection('LeftNavigationTitle', $this->oNavigationModel->getLeftNavigationTitle($this->iLang));
        $this->oViewmanager->setSection('LeftNavigation', $this->oLeftNavigationView->display());
        $this->oViewmanager->setSection('Navigation', $this->oNavigationView->display());
    }
}
